package edu.ubbcs.rs.backend.model.table;

import edu.ubbcs.rs.backend.model.Reservation;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Merli Andras-Bertalan
 * @version 1.0
 * @created 09-jan.-2015 15:37:43
 */
public class CombinedTable extends Table {

    private List<Table> tables;

    public CombinedTable(List<Table> tables) {
        this.tables = tables;
        int minX = tables.get(0).getX();
        int minY = tables.get(0).getY();
        int widthSum = tables.get(0).getWeight();
        int heightSum = tables.get(0).getHeight();
        for (int i = 1; i < tables.size(); i++) {
            widthSum += tables.get(i).getWeight();
//            heightSum += tables.get(i).getHeight();
            if (tables.get(i).getX() < minX) {
                minX = tables.get(i).getX();
                minY = tables.get(i).getY();
            }
        }
        x = minX;
        y = minY;
        weight = widthSum;
        height = heightSum;
    }

    public CombinedTable() {
        this(Collections.<Table>emptyList());
    }

    @Override
    public boolean isReserved(List<Reservation> reservations) {
        for (Table table : tables) {
            if (table.isReserved(reservations)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Reservation> reserve(Reservation reservation) {
        List<Reservation> reservations = new LinkedList<>();
        for (Table table : tables) {
            Reservation r = new Reservation(reservation.getDate(), reservation.getHour(), reservation.getMessage(), reservation.getReserver(), null, reservation.getReservationEvent());
            reservations.addAll(table.reserve(r));
        }
        return reservations;
    }
}
